package cn.lh.spider;

import java.util.Iterator;
import java.util.List;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class BlogPageProcessor implements PageProcessor {

    public static final String URL_LIST = "http://blog\\.sina\\.com\\.cn/s/articlelist_1487828712_0_\\d+\\.html";

    public static final String URL_POST = "http://blog\\.sina\\.com\\.cn/s/blog_\\w+\\.html";

    private Site site = Site
            .me()
            .setDomain("blog.sina.com.cn")
            .setSleepTime(3000)
            .setUserAgent(
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

    public void process(Page page) {
        //列表页
        if (page.getUrl().regex(URL_LIST).match()) {
        	//page.addTargetRequests(page.getHtml().xpath("//div[@class=\"articleList\"]").links().regex(URL_POST).all());
        	page.addTargetRequests(page.getHtml().xpath("//div[@class=\"articleList\"]").links().all());
        	List<String> t =page.getHtml().xpath("//div[@class=\"articleList\"]").links().all();
        	Iterator<String> iter= t.iterator();
        	while(iter.hasNext()){
        		System.out.println("link: "+iter.next());
        	}
            //page.addTargetRequests(page.getHtml().links().regex(URL_LIST).all());
            //文章页
        } else {
            page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2/html()"));
           // page.putField("content", page.getHtml().xpath("//div[@id='articlebody']//div[@class='articalContent']"));
        	//page.putField("date",
            //        page.getHtml().xpath("//div[@id='articlebody']//span[@class='time SG_txtc']")
            //        .regex("\\(([\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:.*)\\)"));
        }
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new BlogPageProcessor()).addUrl("http://blog.sina.com.cn/s/articlelist_1487828712_0_2.html")
        		.addPipeline(new FilePipeline("d://a"))
                .run();
    }
    
    
}